package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.IDictService;
import com.tarena.lbs.common.basic.converter.DictConverter;
import com.tarena.lbs.pojo.basic.bo.DictBO;
import com.tarena.lbs.pojo.basic.param.DictParam;
import com.tarena.lbs.pojo.basic.query.DictQuery;
import com.tarena.lbs.pojo.basic.vo.DictVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 字典基本信息控制层
 */
@RestController
@RequestMapping("/admin/basic/dict/info")
@Api(tags = "字典基本信息模块")
public class DictController {

    @Resource
    private IDictService dictService;

    @Resource
    private DictConverter dictConverter;

    @ApiOperation("字典基本信息新增")
    @PostMapping("/save")
    public Result<Void> save(@RequestBody DictParam dictParam) throws BusinessException {
        dictService.save(dictParam);
        return Result.success();
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable("id") Integer id) {
        dictService.delete(id);
        return Result.success();
    }

    @ApiOperation("字典基本信息更新")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "dictParam", value = "DictParam",
                            required = false, dataType = "DictParam")
            })
    @PutMapping("/update/{id}")
    public Result<Void> update(@PathVariable("id") Integer id, @RequestBody DictParam dictParam) throws BusinessException {
        dictService.update(id, dictParam);
        return Result.success();
    }

    @ApiOperation("字典基本信息详情")
    @GetMapping("/detail/{id}")
    public Result<DictVO> detail(@PathVariable("id") Integer id) {
         DictBO dictBO = dictService.detail(id);
        return new Result<>(dictConverter.bo2vo(dictBO));
    }

    @ApiOperation("字典基本信息分页查询")
    @GetMapping("/list")
    public  Result<PageResult<DictVO>> list(DictQuery dictQuery) {
        PageResult<DictVO> dictVOPageResult = dictService.list(dictQuery);
        return new Result<>(dictVOPageResult);
    }

}
